/**
 * Created by pc on 2016/6/22.
 */
define(['common/js/filter/boss-filter-0.1',
	'common/js/service/pms-common-service-0.1'], function(){
	diretiveModule.directive('noPagingGrid',  function ($injector) {
		return {
			scope: false,
			restrict: "EA",
			replace: true,
			templateUrl: rootPath + '/common/js/html/noPagingGrid.html',
			controller: function ($scope) {},
			link: function(scope, elm, attrs) {

				function requestGridFun() {

					//无服务和具体方法不执行
					if(!attrs.serviceName || !attrs.serviceMethod) {
						console.error("属性service-name/service-method不能为空");
						return;
					}
					scope.requestGridData = $injector.get(attrs.serviceName)[attrs.serviceMethod];

					//attrs.columndefs有坑不能按照外部的驼峰式写法
					scope.columnDefs = scope[attrs.columndefs];
					scope.lastCon = {};

					if(scope[attrs.serviceCondition]){
						scope.lastCon = scope[attrs.serviceCondition];
					}
					scope.requestGridData(scope.lastCon).then(function (callback) {
						
						if(callback != null && callback.totalResults >=0){
							scope.myData = callback.data;
							/*scope[attrs.haveData] = true;*/
							if(callback.totalResults == 0){
								if(attrs.haveMethod && attrs.haveData) {
									scope[attrs.haveMethod](attrs.haveData, false);
								}
							}else{
								if(attrs.haveMethod && attrs.haveData) {
									scope[attrs.haveMethod](attrs.haveData, true);
								}
							}
						} else {
							if(attrs.haveMethod && attrs.haveData) {
								scope[attrs.haveMethod](attrs.haveData, false);
							}
						}
						
					});
				}

				requestGridFun();

				scope.$on('refreshData', function (event, index) {
					if (attrs.refreshFlag == index) {
						requestGridFun();
					}
				});
			}
		}
	});
});